package org.jeecg.modules.utils;

import com.alibaba.fastjson.JSONObject;
import com.xkcoding.http.HttpUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;

/**
 * @description: 微信相关
 * @author: zwf
 * @create: 2025-06-17 14:03
 **/
@Slf4j
public class WeChatUtil {



    /**
     * 获取微信session openid unionid
     *
     * @author zwf
     * @date created in 2025-06-13 08:31
     * @param: code
     * @return: com.alibaba.fastjson.JSONObject
     */
    public static JSONObject getWeChatSession(String sso,String appId,String appSecret,String code) {
        // 构建请求URL
        String url = String.format("%s/sns/jscode2session?appid=%s&secret=%s&js_code=%s&grant_type=authorization_code",
                sso,
                appId,
                appSecret,
                code);
        JSONObject result = new JSONObject();
        try {
            String response = HttpUtil.get(url);
            // 将响应转换为JSONObject
            result = JSONObject.parseObject(response);
            // 检查微信接口返回的错误码
            if (result.containsKey("errcode")) {
                log.error("微信接口错误: " + result.getString("errmsg"));
            }
        } catch (Exception e) {
            log.error("获取微信session失败: " + e.getMessage());
        }
        return result;
    }
}
